/**
 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * updates element arr[i] using fn
 *
 * @param arr an array to run fn over
 *
 * @param fn a function to apply
 * fn takes a value, index of some array and reference to it. Can be used, e.g., to write a function with use of adjacent elements
 */
export function map(arr: {{T}}[], fn: (value: {{T}}, index: int, self: {{T}}[]) => {{T}}): {{T}}[] {
    let res: {{T}}[] = new {{T}}[arr.length];
    for (let i: int = 0; i < arr.length; i++) {
        res[i] = fn(arr[i], i, arr);
    }
    return res;
}

/**
 * creates a new array using fn(arr[i])
 *
 * @param arr an array to run fn over
 *
 * @param fn a function to apply for each element of arr
 *
 * @returns {{T}}[] - a copy of arr where for each element fn was applied
 */
export function map(arr: {{T}}[], fn: (value: {{T}}) => {{T}}): {{T}}[] {
    let res: {{T}}[] = new {{T}}[arr.length];
    for (let i: int = 0; i < arr.length; i++) {
        res[i] = fn(arr[i]);
    }
    return res;
}
